package com.glela.platform.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.glela.common.util.DateUtil;
import com.glela.constant.BizConstant;
import com.glela.platform.mapper.UserJournalMapper;
import com.glela.platform.mapper.UserMoneyMapper;
import com.glela.platform.mapper.UserPromotionMapper;
import com.glela.platform.model.UserJournal;
import com.glela.platform.model.UserMoney;
import com.glela.platform.model.UserPromotion;
import com.glela.platform.service.UserJournalService;
import com.glela.platform.vo.UserJournalVo;

import cn.jpush.api.utils.StringUtils;

@Service("userJournalService")
public class UserJournalServiceImpl implements UserJournalService {

	@Resource
	private UserJournalMapper userJournalMapper;
	@Resource
	private UserPromotionMapper userPromotionMapper;
	@Resource
	private UserMoneyMapper userMoneyMapper;

	/**
	 * 根据用户id查询用户流水账
	 * 
	 * @param userId
	 * @param type
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@Override
	public List<UserJournal> getUserJournalList(long userId, int type, int startIndex, int pageSize) {
		if (BizConstant.DAY_TYPE_ALL == type) {// 全部
			return userJournalMapper.getUserJournalList(userId, startIndex, pageSize);
		} else if (BizConstant.DAY_TYPE_1 == type) {// 本月
			String firstDay = DateUtil.monthFirstDay(0);
			String lastDay = DateUtil.monthLastDay(0);
			return userJournalMapper.getUserJournalListByTime(userId, firstDay, lastDay, startIndex, pageSize);
		} else if (BizConstant.DAY_TYPE_2 == type) {// 上月
			String firstDay = DateUtil.monthFirstDay(1);
			String lastDay = DateUtil.monthLastDay(1);
			return userJournalMapper.getUserJournalListByTime(userId, firstDay, lastDay, startIndex, pageSize);
		} else if (BizConstant.DAY_TYPE_3 == type) {// 上两个月
			String firstDay = DateUtil.monthFirstDay(2);
			String lastDay = DateUtil.monthLastDay(2);
			return userJournalMapper.getUserJournalListByTime(userId, firstDay, lastDay, startIndex, pageSize);
		} else if (BizConstant.DAY_TYPE_4 == type) {// 上三个月
			String firstDay = DateUtil.monthFirstDay(3);
			String lastDay = DateUtil.monthLastDay(3);
			return userJournalMapper.getUserJournalListByTime(userId, firstDay, lastDay, startIndex, pageSize);
		}
		return null;
	}

	/**
	 * 根据id查询用户流水账总数
	 * 
	 * @param userId
	 * @param type
	 * @return
	 */
	@Override
	public int countUserJournal(long userId, int type) {
		if (BizConstant.DAY_TYPE_ALL == type) {// 全部
			return userJournalMapper.countUserJournal(userId);
		} else if (BizConstant.DAY_TYPE_1 == type) {// 本月
			String firstDay = DateUtil.monthFirstDay(0);
			String lastDay = DateUtil.monthLastDay(0);
			return userJournalMapper.countUserJournalByTime(userId, firstDay, lastDay);
		} else if (BizConstant.DAY_TYPE_2 == type) {// 上月
			String firstDay = DateUtil.monthFirstDay(1);
			String lastDay = DateUtil.monthLastDay(1);
			return userJournalMapper.countUserJournalByTime(userId, firstDay, lastDay);
		} else if (BizConstant.DAY_TYPE_3 == type) {// 上两个月
			String firstDay = DateUtil.monthFirstDay(2);
			String lastDay = DateUtil.monthLastDay(2);
			return userJournalMapper.countUserJournalByTime(userId, firstDay, lastDay);
		} else if (BizConstant.DAY_TYPE_4 == type) {// 上三个月
			String firstDay = DateUtil.monthFirstDay(3);
			String lastDay = DateUtil.monthLastDay(3);
			return userJournalMapper.countUserJournalByTime(userId, firstDay, lastDay);
		}
		return 0;
	}

	/**
	 * 根据用户ID获取某个用户的妆币总收入
	 * 
	 * @param userId
	 * @return
	 */
	@Override
	public int getUserJournalTotalIncome(long userId) {
		return userJournalMapper.getUserJournalTotalIncome(userId);
	}

	/**
	 * 查询我的优惠券
	 */
	@Override
	public List<UserPromotion> getUserPromotionList(long userId, int pagelimit, int pageSize) {
		List<UserPromotion> userPromotionList = userPromotionMapper.getUserPromotionListByUserId(userId, pagelimit,
				pageSize);
		return userPromotionList;
	}

	/**
	 * 添加流水账
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public int insertUserJournal(UserJournal userJournal) {
		return userJournalMapper.insertUserJournal(userJournal);
	}
	

	@Override
	public boolean insertSelective(UserJournal userJournal) {
		return userJournalMapper.insertSelective(userJournal)==1;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void insertUserPromotion(int promotionId, int promotionDetailId, int type, String promotionDetailName,
			long userId, int brandId, int goodId, int limited, int isDiscount, Integer amount, int discount,
			String effectiveTime, String expirationTime, int promotionNumber, int targetSelfType) {
		UserPromotion userPromotionDetail = new UserPromotion();
		userPromotionDetail.setPromotionId(promotionId);
		userPromotionDetail.setPromotionDetailId(promotionDetailId);
		userPromotionDetail.setType(type);
		userPromotionDetail.setPromotionDetailName(promotionDetailName);
		userPromotionDetail.setUserId(userId);
		userPromotionDetail.setBrandId(brandId);
		userPromotionDetail.setGoodId(goodId);
		userPromotionDetail.setLimited(limited);
		userPromotionDetail.setIsDiscount(isDiscount);
		userPromotionDetail.setAmount(amount);
		userPromotionDetail.setDiscount(discount);
		userPromotionDetail.setEffectiveTime(effectiveTime);
		userPromotionDetail.setExpirationTime(expirationTime);
		userPromotionDetail.setPromotionNumber(promotionNumber);
		userPromotionDetail.setTargetSelfType(targetSelfType);
		userPromotionMapper.insertUserPromotion(userPromotionDetail);
	}

	/**
	 * 修改持有优惠劵 数量
	 * 
	 * @param Id
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updateUserPromotionNumber(int id) {
		userPromotionMapper.updateUserPromotionNumber(id);
	}

	/**
	 * 查询用户持有的优惠券总数
	 * 
	 * @param userId
	 * @return
	 */
	@Override
	public int countUserPromotionByUserId(long userId) {
		return userPromotionMapper.countUserPromotionByUserId(userId);
	}

	/**
	 * 根据userId查询用户流水账(分页)
	 * 
	 * @param userId
	 * @return
	 */
	@Override
	public List<UserJournalVo> paymentDetails(long userId, int page, int pageSize) throws Exception {
		List<UserJournalVo> userJournalVoList = new ArrayList<UserJournalVo>();
		List<UserJournal> UserJournalList = userJournalMapper.getUserJournalList(userId, page, pageSize);
		for (UserJournal userJournal : UserJournalList) {
			UserJournalVo userJournalVo = new UserJournalVo();
			if (StringUtils.isNotEmpty(userJournal.getCreateTime())) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");// 小写的mm表示的是分钟
				String datestr = userJournal.getCreateTime();
				Date date = sdf.parse(datestr);
				userJournalVo.setWeek(getWeekOfDate(date));
			} else {
				userJournalVo.setWeek("");
			}
			userJournalVo.setCreateTime(userJournal.getCreateTime().substring(11, 16));
			userJournalVo.setType(userJournal.getStatus());
			userJournalVo.setMoney(userJournal.getMoney());
			userJournalVoList.add(userJournalVo);
		}
		return userJournalVoList;
	}

	@Override
	public int countPaymentDetails(Long userId) {
		return userJournalMapper.selecttUserJournalNum(userId);
	}

	private String getWeekOfDate(Date date) {
		String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		// String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" };
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		return weekDaysName[intWeek];
	}

	@Override
	public int batchInsert(List<UserJournal> list) {
		return userJournalMapper.batchInsert(list);
	}
	
	@Override
	public UserMoney getUserMoneyByUserIdAndType(Long userId, int type) {
		List<UserMoney> userMoneyList = this.userMoneyMapper.selectAllByUserIdAndType(userId,type);
		// 如果不存在，则添加一条
		if(CollectionUtils.isEmpty(userMoneyList)){
			UserMoney userMoney = new UserMoney();
			userMoney.setUserId(userId);
			userMoney.setType(type);
			userMoney.setNormalAmount(0l);
			userMoney.setFrozenAmount(0l);
			userMoney.setCreateTime(DateUtil.parse(DateUtil.currentStringDate()));
			this.userMoneyMapper.insertSelective(userMoney);
			return userMoney;
		}
		return userMoneyList.get(0);
	}
	
	@Override
	public List<UserJournal> getMyBalanceListByPage(Long userId, Date beginDate,
			Date endDate, int pageLimit, int pageSize) {
		return this.userJournalMapper.getMyBalanceListByPage(userId,beginDate,endDate,pageLimit,pageSize);
	}

	@Override
	public List<UserPromotion> getUserPromotionListWhichNotUsed(Long userId, int pageLimit, int pageSize) {
		return userPromotionMapper.getUserPromotionListWhichNotUsed(userId,pageLimit,pageSize);
	}
	
	@Override
	public List<UserPromotion> getUserPromotionListWhichHasUsed(Long userId, int pageLimit, int pageSize) {
		return userPromotionMapper.getUserPromotionListWhichHasUsed(userId,pageLimit,pageSize);
	}
	
	@Override
	public List<UserPromotion> getUserPromotionListWhichOverdue(Long userId, int pageLimit, int pageSize) {
		return userPromotionMapper.getUserPromotionListWhichOverdue(userId,pageLimit,pageSize);
	}
	
	@Override
	public int getUserPromotionTotalAmountWhichNotUsed(Long userId) {
		return userPromotionMapper.getUserPromotionTotalAmountWhichNotUsed(userId);
	}

	@Override
	public UserJournal getUserJournalByBusinessTypeAndCommisionID(
			int businessTypeId, long commisionId) {
		return userJournalMapper.getUserJournalBySourceIDandBusinessType(
						commisionId,businessTypeId);
	}

	@Override
	public int checkOnWayAmountNear30Days(Long userId) {
		String after30daysTime = DateUtil.getFewDay(-30);
		return this.userJournalMapper.checkOnWayAmountNear30Days(userId,after30daysTime);
	}
 
}
